package com.rx.mgt.controller;

import com.rx.mgt.domain.*;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.service.DomainService;
import com.rx.mgt.service.EmailListService;
import com.rx.mgt.service.SysLogService;
import com.rx.mgt.service.UserInfoExtService;
import com.rx.mgt.util.IpUtil;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * Created by zxj on 2017/5/25.
 */
@Controller
public class EmailListController {
    @Autowired
    DomainService domainService;

    @Autowired
    private EmailListService emailListService;
    @Autowired
    SysLogService sysLogService;
    @Autowired
    UserInfoExtService userInfoExtService;
    @RequestMapping("email-list.htmlx")
    public String emailList(HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        String domain = request.getParameter("domain");
        UsersBo usersBo = new UsersBo();
        List domainList = domainService.getDomainInfoExt(usersBo);
        if(StringUtils.isNotBlank(domain)){
            usersBo.setDomain(domain);
        }else{
            usersBo.setDomain(((Domain)domainList.get(0)).getDomain_name());
        }
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        List<EmailListDomain> list = emailListService.queryEmailList(usersBo);
        request.setAttribute("list",list);
        request.setAttribute("domainList",domainList);
        request.setAttribute("dom",domain);
        return "email/email-list";
    }
    //**添加邮件列表页面

    @RequestMapping("email-list-add.htmlx")
    public String addEmailList(HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        String domain = request.getParameter("domain");
        UsersBo  usersBo = new UsersBo();
        List list = domainService.getDomainInfoExt(usersBo);
        if(StringUtils.isBlank(domain)){
            domain = user.getDomain();
        }
        request.setAttribute("dom",domain);
        request.setAttribute("domainList",list);
        return "email/email-list-add";
    }
    /***
     * 添加邮件列表
     */
    @RequestMapping("/emailListSave.htmlx")
    @ResponseBody
    public String emailListSave(EmailListDomain model){
        String domain = model.getDomain();
        model.setListAddress(model.getListAddress()+"@"+domain);
        emailListService.saveEmailList(model);
        return "success";

    }

    /**
     * 维护邮件列表
     */
    @RequestMapping("/email-list-member-add.htmlx")
    public String emailListMember(@RequestParam("id")String id, ModelMap modelMap){
        List<EmailListMember> list = emailListService.queryEmailMemberListByEmailListId(id);

        modelMap.addAttribute("list",list);
        return "email/email-list-member-list";
    }

    /**
     * 邮件列表修改
     */
    @RequestMapping("/email-list-update.htmlx")
    public String emailListEdit(@RequestParam("id")String id, ModelMap model){
        EmailListDomain emailListDomain = emailListService.queryEmailListDomain(id);
        model.addAttribute("model",emailListDomain);
        return "email/email-list-update";
    }
    /**
     * 修改邮件列表信息
     */
    @RequestMapping("/emailListUpdate.htmlx")
    @ResponseBody
    public String emailListUpdate(EmailListDomain model,HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("修改邮件列表信息", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        emailListService.updateEmailListDomain(model);
        return "success";
    }
    /**
     * 删除邮件列表
     */
    @RequestMapping("/deleteEmailList.htmlx")
    @ResponseBody
    public String deleteEmailList(@RequestParam("id")String id,HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("删除邮件列表信息", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        emailListService.deleteEmailListById(id);
        return  "success";
    }
    /**
     * 邮件列表添加成员时验证是否存在该用户
     */
    @RequestMapping("/checkUser.htmlx")
    @ResponseBody
    public  String checkUser(@RequestParam("email")String email,HttpServletRequest request,@RequestParam("emailListId")String emailListId){
        EmailListDomain emailListDomain = emailListService.queryEmailListDomain(emailListId);
        String domian = emailListDomain.getDomain();
        UserInfoExt ext = emailListService.getUserByDomainAndEmail(domian,email);
        if(ext==null){
            return "001";
        }else{
            EmailListMember member = new EmailListMember();
            member.setEmail(email);
            member.setEmailListId(Long.valueOf(emailListId));
            member.setInsertDate(new Date());
            member.setType("1");
            EmailListMember emailListMemberByEmail = emailListService.getEmailListMemberByEmail(email);
            if(emailListMemberByEmail==null){
                return "002";
            }
            AdminUser user = (AdminUser) request.getSession().getAttribute("user");
            sysLogService.add("添加邮件列表成员", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
            emailListService.addEmailToEmailListMember(member);
        }
        return "003";
    }
    /**
     * 修改邮件列表成员
     */
    @RequestMapping("/email-list-member-update.htmlx")
    public String updateEmailListMember(HttpServletRequest request){
        String id = request.getParameter("id");
        EmailListMember member = emailListService.queryEmailListMemberById(id);
        request.setAttribute("model",member);
        return "email/email-list-member-update";
    }

    /**
     * 更新邮件列表成员
     */
    @RequestMapping("/emailListMemberUpdate.htmlx")
    @ResponseBody
    public String emailListMemberUpdate(HttpServletRequest request,EmailListMember member){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        member.setInsertDate(new Date());
        sysLogService.add("修改邮件列表成员", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        emailListService.updateEmailListMember(member);

        return "success";
    }
    /**
     * 删除邮件列表成员
     */
    @RequestMapping("/deleteEmailListMember.htmlx")
    @ResponseBody
    public String deleteEmailListMember(HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        emailListService.deleteEmailListMemberById(request.getParameter("id"));
        sysLogService.add("删除邮件列表成员", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        return "success";
    }

}
